package com.example.leetdemo.finash;

import java.util.Arrays;

public class Solution2966 {


    public int[][] divideArray(int[] nums, int k) {
        if (nums == null || nums.length == 0) {
            return new int[][]{};
        }
        int t = nums.length / 3;
        int[][] a = new int[t][3];
        Arrays.sort(nums);


        for (int i = 0; i < t; i++) {
            if (nums[ 3 * (i + 1) - 1] - nums[3 * i] > k) {
                return new int[][]{};
            }
        }
        for (int i = 0; i < t; i++) {
            for (int j = 0; j < 3; j++) {
                a[i][j] = nums[j + 3 * i];
            }
        }
        return a;
    }
}
